package org.hilo.boot.core.util;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Collection Tools
 * 
 * @author zollty
 * @since 2016-12-03
 */
public class CollectionTools {

    /**
     * 实现类似于 sql的 order by 多个字段的功能。
     * @param comparatorList 按顺序指定多个比较器
     */
    @SafeVarargs
    public static <T> void sort(List<T> list, final Comparator<T>... comparatorList) {
        if (comparatorList == null || comparatorList.length == 0) {
            // throw new IllegalArgumentException("comparatorList is empty.");
            return;
        }
        Comparator<T> comparator = new Comparator<T>() {
            @Override
            public int compare(T o1, T o2) {
                for (Comparator<T> c : comparatorList) {
                    if (c.compare(o1, o2) > 0) {
                        return 1;
                    }
                    else if (c.compare(o1, o2) < 0) {
                        return -1;
                    }
                }
                return 0;
            }
        };
        Collections.sort(list, comparator);
    }
}
